package com.putao.tinytime.activities;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.putao.tinytime.R;
import com.putao.tinytime.adapter.SenceViewPagerAdapter;
import com.putao.tinytime.base.BaseActivity;
import com.putao.tinytime.bean.CollageConfigInfo;
import com.putao.tinytime.event.BasePostEvent;
import com.putao.tinytime.event.EventBus;
import com.putao.tinytime.fragments.SenceFragment;
import com.putao.tinytime.util.ActivityHelper;
import com.putao.tinytime.util.CollageInfoManager;
import com.putao.tinytime.util.Loger;
import com.putao.tinytime.util.PuTaoConstants;
import com.putao.tinytime.view.SenceViewPage;

import java.util.ArrayList;

public class EditActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private CollageInfoManager mcollageInfoManager;
    private Button back_btn;
    private SenceViewPage sence_pager;
    private ArrayList<SenceFragment> fragmentList = new ArrayList<>();
    private int selected_page_index = 0;

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_edit;
    }

    @Override
    public void doInitSubViews(View view) {
        back_btn = (Button) findViewById(R.id.back_btn);
        sence_pager = (SenceViewPage) findViewById(R.id.sence_pager);
        tv_title = (TextView) findViewById(R.id.tv_title);
        addOnClickListener(back_btn);
        EventBus.getEventBus().register(this);
    }

    public void disableViewPagerScroll(boolean enable) {
        sence_pager.setPagingEnabled(!enable);
    }

    @Override
    public void doInitData() {
        mcollageInfoManager = CollageInfoManager.getInstance();
        CollageConfigInfo info = mcollageInfoManager.getCollageConfigInfo();
        ArrayList<CollageConfigInfo.SenceItemInfo> list = info.data.get(0).scenceElements;
        for (int i = 0; i < list.size(); i++) {
            SenceFragment fragment = new SenceFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("item", list.get(i));
            bundle.putString("sample_bg", info.data.get(0).sample_bg);
            fragment.setArguments(bundle);
            fragmentList.add(fragment);
        }
        sence_pager.setAdapter(new SenceViewPagerAdapter(getSupportFragmentManager(), fragmentList));
        sence_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                selected_page_index = i;
                tv_title.setText((selected_page_index + 1) + "/" + fragmentList.size());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        tv_title.setText((selected_page_index + 1) + "/" + fragmentList.size());
    }

    public void startSelectPhoto() {
        ActivityHelper.startActivity(mActivity, PhotoSelectActivity.class);
    }

    public void onEvent(BasePostEvent event) {
        switch (event.eventCode) {
            case PuTaoConstants.EVENT_CODE_SELECT_PHOTO:
                Bundle bundle = event.bundle;
                Loger.d("path===>" + bundle.getString("photo_path"));
                fragmentList.get(selected_page_index).setPhoto(bundle.getString("photo_path"));
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }
}
